package com.xbongbong.saas.model.impl;/*/*/

import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.help.subform.TransferSubFormHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.domain.dao.WorkOrderCcDao;
import com.xbongbong.saas.domain.entity.WorkOrderCcEntity;
import com.xbongbong.saas.model.WorkOrderCcModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
 *
 * @author 魏荣杰
 * @date 2019/4/3 15:17
 * @since v1.0
 * @version v1.0
 */
@Service("workOrderCcModel")
public class WorkOrderCcModelImpl implements WorkOrderCcModel {
    @Resource
    private WorkOrderCcDao workOrderCcDao;
    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private TransferSubFormHelper transferSubFormHelper;

    @Override
    public Integer insert(WorkOrderCcEntity entity) throws XbbException {
        Long now = DateTimeUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        Integer result = workOrderCcDao.insert(entity);
        PaasFormDataEntity paasFormDataEntity = transferSubFormHelper.transferUserTeamToFormData(IndexTypeEnum.IDX_SAAS_WORK_ORDER_CC, entity);
        InsertDTO saveDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_WORK_ORDER_CC);
        paasEsModel.insert(saveDTO);
//        paasEsModel.insert(entity,IndexTypeEnum.IDX_SAAS_WORK_ORDER_CC,entity.getCorpid(),entity.getId(),entity.getWorkOrderId());
        return result;
    }

    @Override
    public Integer update(WorkOrderCcEntity entity) throws XbbException {
        entity.setUpdateTime(DateTimeUtil.getInt());
        Integer update = workOrderCcDao.update(entity);
        PaasFormDataEntity paasFormDataEntity = transferSubFormHelper.transferUserTeamToFormData(IndexTypeEnum.IDX_SAAS_WORK_ORDER_CC, entity);
        InsertDTO saveDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_WORK_ORDER_CC);
        paasEsModel.update(saveDTO);
        return update;
    }

    @Override
    public List<WorkOrderCcEntity> findEntitys(Map<String, Object> param) {
        return workOrderCcDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return workOrderCcDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public List<String> getUserIds(String corpid, Long workOrderId) {
        return workOrderCcDao.getUserIds(corpid, workOrderId);
    }

    @Override
    public Integer save(WorkOrderCcEntity entity) throws XbbException {
        if (entity.getId() == null || entity.getId().equals(0L)) {
            return insert(entity);
        }
        return update(entity);
    }
}
